var existenceInstruments = ['guzheng'],       //已存在的乐器                ARRAY
    existenceScale = ['C'],                   //已存在的音阶                ARRAY
    existenceNote = ['1','2','3','4','5','6','7','1-','2-','3-','4-','5-','6-','7-','1+','2+','3+','4+','5+','6+','7+','1++']; //存在的音符    ARRAY

/**
 * 自动演奏初始化
 */
function autoInit() {
    let audioContent = '<audio>';

    for (let i in existenceInstruments){
        for (let s in existenceScale){
            for (let n in existenceNote){
                let fileName = '', scale = existenceScale[s], instruments = existenceInstruments[i], note = existenceNote[n], nowkey = rkeys[note];
                fileName += scale + '$' + note + '$' + nowkey + '.mp3';

                audioContent += '<source id="' + instruments + '-' + scale +'-code-'+ note +'" src="./notes/' + instruments + '/' + scale + '/' + fileName + '"></source>';
            }
        }
    }

    audioContent += '</audio>';

    $('#audiocontent').html(audioContent);
}

/**
 * 自动演奏的直接演奏方式
 * @param id array 需要弹奏的DOM id
 */
function autoGoPlay(id) {
    for (let b in id){
        if (document.getElementById(id[b]).paused) {//是否暂停
            document.getElementById(id[b]).play();
        } else {
            //未暂停则设置一个定时删除的播放器
            let nowSrc = document.getElementById(id[b]).getAttribute('src'),
                nowId = 'code-' + randomStr(20),
                tmpAudio = '<audio id="' + nowId + '"><source src="' + nowSrc + '"></source></audio>';
    
            $('#audiocontent').append(tmpAudio);
            document.getElementById(nowId).play();
            setTimeout(function () {
                $('#' + nowId).remove();
            }, 5000)
        }
    }
}

/**
 * 开始播放
 */
function playstart() {
    $('#doplay').hide();
    $('#dopause').show();
    $('#dostop').show();
    $("input[name='stopflag']").val('0');
    players(allKeyNum);//执行播放
    //让音轨动起来
    $('#trackdetail').animate(
        { 'left': '-' + (scoreLength - pianoCodeLength + corrections).toString() },
        parseInt($("input[name='speed']").val()) * (parseInt(allKeyNum) + parseInt(speedCorre)),
        'linear'
    )
}
/**
 * 停止播放
 */
function playstop() {
    $('#doplay').show();
    $('#dopause').hide();
    $('#dostop').hide();
    $("#docontinue").hide();
    $("input[name='stopflag']").val('1');
    $("input[name='pauseflag']").val('0');//重置键位

    $('#trackdetail').stop();
    resetScore();
}
/**
 * 暂停播放
 */
function playpause() {
    $("#docontinue").css({ 'display': "inline" });
    $('#dopause').hide();
    $("input[name='stopflag']").val('1');

    $('#trackdetail').stop();
    $('#trackdetail').css({ 'left': '-' + $("input[name='pauseflag']").val() * 100 + pianoCodeLength });
}
/**
 * 继续播放
 */
function playcontinue() {
    $("#docontinue").hide();
    $('#dopause').css({ 'display': "inline" });
    $("input[name='stopflag']").val(0);
    players(allKeyNum);

    $('#trackdetail').animate(
        { 'left': '-' + (scoreLength - pianoCodeLength + corrections).toString() },
        parseInt($("input[name='speed']").val()) * ((parseInt(allKeyNum) + parseInt(speedCorre)) - parseInt($("input[name='pauseflag']").val())),
        'linear'
    )
}